package com.systemdevelopment.mainproject.View.Visitor;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXTextField;
import com.systemdevelopment.mainproject.Model.Book;
import com.systemdevelopment.mainproject.View.oneInstanceController;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Pagination;
import javafx.scene.control.TextFormatter;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.util.Callback;

import javax.lang.model.type.ArrayType;
import java.util.ArrayList;
import java.util.HashSet;

public class SearchPaneController extends oneInstanceController {
    ArrayList<bookPane> books= new ArrayList<>(10);

    @FXML
    private JFXTextField searchText;

    @FXML
    private JFXButton searchButton;

    @FXML
    private Pagination pagination;

    @FXML
    private HBox searchBox;

    @FXML
    void searchStart(ActionEvent event) {
        //books.add()
        String target=searchText.getText();
        ArrayList<bookPane> books=new ArrayList<>();
        HashSet<Book> res=Book.getBookFromDBByName(target);
        for(Book bk:res)
        {
            books.add(new bookPane(bk));
        }
        int size=books.size();

        pagination.setMaxPageIndicatorCount(size/10+1);
        pagination.setPageCount(size/10+1);
        pagination.setPageFactory(param -> {
            VBox pane=new VBox();
            pane.setPrefHeight(400);
            pane.setPrefWidth(700);
            HBox fristBox=new HBox();
            HBox secondBox=new HBox();
            fristBox.setPrefHeight(200);
            fristBox.setPrefWidth(700);
            secondBox.setPrefWidth(700);
            secondBox.setPrefHeight(200);

            for(int i=10*param;i<size&&i<10*param+5;i++)
            {
                fristBox.getChildren().add(books.get(i).getPane());
            }
            for(int i=10*param+5;i<size&&i<10*param+10;i++)
            {
                secondBox.getChildren().add(books.get(i).getPane());
            }
            pane.getChildren().add(fristBox);
            pane.getChildren().add(secondBox);
            return pane;
        });

        pagination.setVisible(true);


    }
    public void loadAllBooks()
    {
        ArrayList<bookPane> books=new ArrayList<>();
        HashSet<Book> res=Book.getAllBooksFromDB();
        for(Book bk:res)
        {
            books.add(new bookPane(bk));
        }
        int size=books.size();

        pagination.setMaxPageIndicatorCount(size/10+1);
        pagination.setPageCount(size/10+1);
        pagination.setPageFactory(param -> {
            VBox pane=new VBox();
            pane.setPrefHeight(400);
            pane.setPrefWidth(700);
            HBox fristBox=new HBox();
            HBox secondBox=new HBox();
            fristBox.setPrefHeight(200);
            fristBox.setPrefWidth(700);
            secondBox.setPrefWidth(700);
            secondBox.setPrefHeight(200);

            for(int i=10*param;i<size&&i<10*param+5;i++)
            {
                fristBox.getChildren().add(books.get(i).getPane());
            }
            for(int i=10*param+5;i<size&&i<10*param+10;i++)
            {
                secondBox.getChildren().add(books.get(i).getPane());
            }
            pane.getChildren().add(fristBox);
            pane.getChildren().add(secondBox);
            return pane;
        });

        pagination.setVisible(true);
    }
    public SearchPaneController()
    {
        searchText=new JFXTextField();
        searchButton=new JFXButton();
        pagination=new Pagination();
        searchBox=new HBox();
        pagination.setVisible(false);
        searchBox.getChildren().add(searchText);
        searchBox.getChildren().add(searchButton);
    }
}
